package net.goutalk.glcs.module.erpModel.caseErpCustomer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.dto.AddCaseErpCustomerGatherDetailDto;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.dto.UpdateCaseErpCustomerGatherDetailDto;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.entity.CaseErpCustomerGather;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.entity.CaseErpCustomerGatherDetail;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.mapper.CaseErpCustomerGatherDetailMapper;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.mapper.CaseErpCustomerGatherMapper;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.service.ICaseErpCustomerGatherDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 客户回款详情【case_erp_customer_gather_detail】 服务实现类
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-12
 */
@Service
@AllArgsConstructor
public class CaseErpCustomerGatherDetailServiceImpl extends ServiceImpl<CaseErpCustomerGatherDetailMapper, CaseErpCustomerGatherDetail> implements ICaseErpCustomerGatherDetailService {

    private final CaseErpCustomerGatherMapper caseErpCustomerGatherMapper;

    @Override
    public Boolean add(AddCaseErpCustomerGatherDetailDto dto) {
        CaseErpCustomerGatherDetail caseErpCustomerGatherDetail = BeanUtil.toBean(dto, CaseErpCustomerGatherDetail.class);
        this.save(caseErpCustomerGatherDetail);

        CaseErpCustomerGather gather = caseErpCustomerGatherMapper.selectById(dto.getGatherId());

        //未还款减去本次还款 等于下一次的未还款
        BigDecimal subtract = gather.getUnpaidAmount().subtract(caseErpCustomerGatherDetail.getAmountCollect());
        gather.setUnpaidAmount(subtract);
        //将已还款新增 本次还款  等于 下一次的已还款
        BigDecimal alreadyAmount = gather.getAlreadyAmount();
        if (ObjectUtil.isNotEmpty(alreadyAmount)){
            gather.setAlreadyAmount(alreadyAmount.add(caseErpCustomerGatherDetail.getAmountCollect()));
        }else {
            gather.setAlreadyAmount(dto.getAmountCollect());
        }
        caseErpCustomerGatherMapper.updateById(gather);
        return true;
    }

    @Override
    public Boolean edit(UpdateCaseErpCustomerGatherDetailDto dto) {
        CaseErpCustomerGatherDetail caseErpCustomerGatherDetail = BeanUtil.toBean(dto, CaseErpCustomerGatherDetail.class);

        CaseErpCustomerGather gather = caseErpCustomerGatherMapper.selectById(dto.getGatherId());

        //原来的详情信息
        CaseErpCustomerGatherDetail oldDetail = this.getById(dto.getId());

        //加回原数据
        gather.setUnpaidAmount(gather.getUnpaidAmount().add(oldDetail.getAmountCollect()));

        //再减去修改后的
        BigDecimal subtract = gather.getUnpaidAmount().subtract(dto.getAmountCollect());
        gather.setUnpaidAmount(subtract);

        //减去原有的
        gather.setAlreadyAmount(gather.getAlreadyAmount().subtract(oldDetail.getAmountCollect()));

        gather.setAlreadyAmount(gather.getAlreadyAmount().add(dto.getAmountCollect()));

        this.updateById(caseErpCustomerGatherDetail);
        caseErpCustomerGatherMapper.updateById(gather);
        return true;
    }

    @Override
    public Boolean delete(Long id) {
        CaseErpCustomerGatherDetail caseErpCustomerGatherDetail = this.getById(id);
        CaseErpCustomerGather caseErpCustomerGather = caseErpCustomerGatherMapper.selectById(caseErpCustomerGatherDetail.getGatherId());
        //未回款
        caseErpCustomerGather.setUnpaidAmount(caseErpCustomerGather.getUnpaidAmount().add(caseErpCustomerGatherDetail.getAmountCollect()));
        //已回款
        caseErpCustomerGather.setAlreadyAmount(caseErpCustomerGather.getAlreadyAmount().subtract(caseErpCustomerGatherDetail.getAmountCollect()));
        caseErpCustomerGatherMapper.updateById(caseErpCustomerGather);
        return this.removeById(id);
    }


}
